<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%--
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
--%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="javax.mail.*"%>
<%@page import="javax.mail.internet.*,java.util.*;"%>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>邮件内容</title>
	</head>
	<body>
	<h1>收件箱</h1>
		<%
			String host = "127.0.0.1";
			String user = request.getParameter("user");
			String password = request.getParameter("password");
			String protocol = request.getParameter("protocol");
			Message message[]=null;
			Store store = null;
			//String url=null;
			//String root=null;
			try {
				Properties props = System.getProperties();
				Session sess = Session.getDefaultInstance(props, null);
				sess.setDebug(true);
				
				store = sess.getStore(protocol);
				store.connect(host, user, password);
				Folder folder = store.getFolder("INBOX");
				folder.open(Folder.READ_ONLY);
				message = folder.getMessages();
				int size = message.length;
				if (size == 0) {
					RequestDispatcher rd = request
							.getRequestDispatcher("/emptyMailBox.jsp");
					rd.forward(request, response);
				}
			} catch (MessagingException ex) {
				ex.printStackTrace();
			}
			
			for(int i = message.length-1;i>=0;i--)
			{Message mm = message[i];
		%>
		<div class="mail">
			<h2><%= mm.getSubject() %></h2>
			<p><strong style="font-size:0.7em">发送时间:<%=mm.getSentDate().toString()%><br/>
			   From:<%=mm.getFrom()[0].toString() %><br/><br/>
			   </strong>
			   <%=mm.getContent() %>
			</p>
			<hr/>
			</div>
			
						<%
						}
						store.close();
							out.println("<a href='mailservice.jsp'>返回邮件服务！");
						%>
		<%--
    This example uses JSTL, uncomment the taglib directive above.
    To test, display the page like this: index.jsp?sayHello=true&name=Murphy
    --%>
		<%--
    <c:if test="${param.sayHello}">
        <!-- Let's welcome the user ${param.name} -->
        Hello ${param.name}!
    </c:if>
    --%>

	</body>
</html>
